package com.gosling.gosling;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.PhoneUtil;
import cn.hutool.core.util.StrUtil;
import com.github.yitter.contract.IdGeneratorOptions;
import com.github.yitter.idgen.YitIdHelper;
import com.google.common.collect.ImmutableMap;
import com.gosling.cloudspace.common.entity.dtos.OrderInfoDto;
import com.gosling.cloudspace.common.entity.pojo.UserInfoPO;
import com.gosling.cloudspace.common.service.ExcelDataQueryService;
import com.gosling.cloudspace.tools.MD5Tool;
import org.apache.commons.codec.digest.DigestUtils;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.function.Supplier;

public class Test20231226 {

    static ExcelDataQueryService excelDataQueryService;

    private static Supplier<Exception> tryExp(String msg) {
        return () -> new Exception(msg);
    }

    public static void main(String[] args) throws Exception {


    }


    private static void extracted1() {
        //        String data = "K8FQ9GVUZO171411333916217008196969firewallsSm3&#%$@!(%)…";
        String data = "123123&1";
//        String data = "1234568910哈哈";
        System.out.println(DigestUtils.md5Hex(data));
        System.out.println(MD5Tool.encode(data));
        System.out.println(MD5Tool.generate(data));
        System.out.println(MD5Tool.getMd5ByFail(data));
        System.out.println(MD5Tool.funMD5(data));

        HashMap<Integer, Integer> dateDayPostpone = new HashMap<>();
        dateDayPostpone.put(26, 1);
        dateDayPostpone.put(27, 2);
        dateDayPostpone.put(28, 3);
        dateDayPostpone.put(29, 4);
        dateDayPostpone.put(30, 5);
        dateDayPostpone.put(31, 6);

        boolean moreThan = new BigDecimal("1000").compareTo(new BigDecimal("1000")) >= 0;
        System.out.println(moreThan);
    }

    private static void asd() {
        OrderInfoDto bean = new OrderInfoDto();
        OrderInfoDto bean2 = new OrderInfoDto();
        bean.setOrderName("123456");
        bean.setList(new ArrayList<>());
        System.out.println(BeanUtil.beanToMap(bean, false, true));
        Map<String, Object> map = BeanUtil.beanToMap(bean2, false, true);
        boolean empty = CollUtil.isEmpty(map);
        System.out.println(map);
        System.out.println(empty);
    }

    private static void test01() {
        List<OrderInfoDto> list = excelDataQueryService.pageDataList(1, 10);
        List<UserInfoPO> list2 = excelDataQueryService.pageDataList(1, 10);
    }

    private static void memoryvoid() {
        Runtime runtime = Runtime.getRuntime();
        int cores = runtime.availableProcessors();
        long freeMemory = runtime.freeMemory();
        long totalMemory = runtime.totalMemory();
        long maxMemory = runtime.maxMemory();
        new Thread(() -> {
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("线程结束");
        }).start();
        int activeThreadCount = Thread.activeCount();
        System.out.println("当前活动线程数：" + activeThreadCount);
        System.out.println("CPU核心数：" + cores);
        System.out.println("freeMemory：" + freeMemory / 1024 / 1024 + "M");
        System.out.println("totalMemory：" + totalMemory / 1024 / 1024 + "M");
        System.out.println("maxMemory：" + maxMemory / 1024 / 1024 + "M");
    }

    private static boolean between(TreeMap<BigDecimal, BigDecimal> ranges, BigDecimal newMin, BigDecimal newMax) {
        if (ranges.lowerEntry(newMin) != null
                && ranges.lowerEntry(newMin).getValue().compareTo(newMin) >= 0
                || ranges.higherEntry(newMin) != null
                && ranges.higherEntry(newMin).getKey().compareTo(newMax) <= 0) {
            return true;
        } else {
            ranges.put(newMin, newMax);
        }
        return false;
    }

    private static void longToDate() {
        long se = Long.parseLong("1713863822317");
        Date date = new Date(se);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String formattedDate = sdf.format(date);
        LocalDateTime localDateTime = DateUtil.parseLocalDateTime(formattedDate);
        System.out.println(localDateTime);
    }

    /**
     * 将元数据前补要，补后的总长度为指定的长度，以字符串的形式返回
     *
     * @param numLength 字符总长度
     * @param randomNum 随机数
     * @return 重组后的数据
     **/
    public static String supplementZero(int numLength, int randomNum) {
        return String.format("%0" + numLength + "d", randomNum);
    }

    private static void immuMap() {
        Map<String, String> map = ImmutableMap.of("1", "1", "2", "2");

        if (!StrUtil.isAllNotBlank("1", "1", "")) {
            System.out.println(111);
        }
    }

    private static void extracted() {
        String[] a = {"1"};
        List<Object> list = Arrays.asList(a);
        list.add("2");
        list.set(1, "2");

        a[0] = "2";
        System.out.println(list);

//        String a = "1";
//        ArrayList<String> x = new ArrayList<>();
//        x.add(a);
//        ArrayList<String> y = new ArrayList<>();
//        y.add(a);
//        x.removeAll(y);
//        System.out.println(x);
//        System.out.println(y);

//        test33();
    }

    private static void test33() {
        List<Long> ids1 = Arrays.asList(1L, 3L, 2L);
        List<Long> ids2 = Collections.singletonList(2L);
        List<Long> ids3 = new ArrayList<>();
        ids3.add(1L);
        ids3.add(2L);
        List<Long> ids = new ArrayList<>();
        ids.add(2L);
        System.out.println("==== 001");
        ids1.removeAll(ids); // 这一步会报错
        System.out.println("==== 002");
        ids2.removeAll(ids); // 这一步也会报错
        System.out.println("==== 003");
        ids3.removeAll(ids);
    }

    private static void uuid() {
        String A = "LOAN";
        IdGeneratorOptions options = new IdGeneratorOptions((short) 4);
        // options.WorkerIdBitLength = 10; // 默认值6，限定 WorkerId 最大值为2^6-1，即默认最多支持64个节点。
        // options.SeqBitLength = 6; // 默认值6，限制每毫秒生成的ID个数。若生成速度超过5万个/秒，建议加大 SeqBitLength 到 10。
        // options.BaseTime = Your_Base_Time; // 如果要兼容老系统的雪花算法，此处应设置为老系统的BaseTime。
        // ...... 其它参数参考 IdGeneratorOptions 定义。

        // 保存参数（务必调用，否则参数设置不生效）：
        YitIdHelper.setIdGenerator(options);
        for (int i = 0; i < 100; i++) {
            new Thread(() -> {
                String sss = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
                long newId = YitIdHelper.nextId();
                System.out.println(A + sss + newId);
            }).start();
        }
    }

    private static void aaaa() {

        new OrderInfoDto();
    }


    private static LocalDate nexDate(LocalDate now, int dayOfMonth, int monthPlus) {
        int year = now.getYear();
        if (monthPlus > 12) {
            return LocalDate.of(year + 1, monthPlus - 12, dayOfMonth);
        }
        return LocalDate.of(year, monthPlus, dayOfMonth);
    }

    private static LocalDate nexDate2(LocalDate now, int dayOfMonth, int monthPlus) {
        return now.withDayOfMonth(dayOfMonth).plusMonths(monthPlus);
    }

    private static void test022() {
        OrderInfoDto a = new OrderInfoDto();
        a.setOrderName("1");
        test020(a);
        System.out.println(a.getOrderName());
    }

    private static void test020(OrderInfoDto a) {
        OrderInfoDto b = new OrderInfoDto();
        b.setOrderName("5");
        a = b;

        a.setOrderName("5");
    }

    private static void test021(String a) {
        a = "5";
    }

    public static boolean isPhone(CharSequence value) {
        return PhoneUtil.isTel400800(value) || PhoneUtil.isMobileHk(value) || PhoneUtil.isMobileTw(value) || PhoneUtil.isMobileMo(value);
    }

    private static void test019() {
//        System.out.println(PhoneUtil.isMobile("00000000"));
//        System.out.println(PhoneUtil.isPhone("00000000"));
        System.out.println(PhoneUtil.isTel400800("00000000"));
        System.out.println(PhoneUtil.isTel("00000000"));

//
//        List<Long> list = new ArrayList<>();
//        for (int i = 0; i < 7; i++) {
//            list.add(Long.valueOf(i));
//        }
//        List<List<Long>> partitionList = Lists.partition(list, 2);
//        for (List<Long> longs : partitionList) {
//            for (int i = longs.size() - 1; i >= 0; i--) {
//                list.remove(longs.get(i));
//            }
////            for (Long aLong : longs) {
////                list.remove(aLong);
////            }
//        }
//        LocalDateTime startTime = LocalDateTime.of(2023, 2,1,0,0,0 );
//        LocalDateTime endTime = LocalDateTime.of(2023, 2,1,2,0,0 );
//
//        LocalDateTime startTime2 = LocalDateTime.of(2023, 2,1,2,0,0 );
//        LocalDateTime endTime2 = LocalDateTime.of(2023, 2,1,4,0,0 );
//
//        LocalDateTime time = LocalDateTime.of(2023, 2,1,2,0,0 );
//        System.out.println(isCurrentIntervalTime(startTime, endTime, time));
//
//        System.out.println(isCurrentIntervalTime(startTime2, endTime2, time));
    }

    public static boolean isCurrentIntervalTime(LocalDateTime startTime, LocalDateTime endTime, LocalDateTime time) {
        if ((startTime.isBefore(time) || startTime.isEqual(time)) && (time.isBefore(endTime) || time.isEqual(endTime))) {
            return true;
        }
        return false;
    }

    /***
     * 循环嵌套解决方案
     */
    private static void innerForEach() {
        List<UserInfoPO> list = new ArrayList<>();
        UserInfoPO userInfoPO = new UserInfoPO();
        userInfoPO.setName("唐三");
        userInfoPO.setImage("哈哈哈");
        list.add(userInfoPO);
        userInfoPO = new UserInfoPO();
        userInfoPO.setName("韩立");
        list.add(userInfoPO);
        List<UserInfoPO> list2 = new ArrayList<>();
        userInfoPO = new UserInfoPO();
        userInfoPO.setName("历飞宇");
        list2.add(userInfoPO);
        userInfoPO = new UserInfoPO();
        userInfoPO.setName("唐三");
        list2.add(userInfoPO);

        HashMap<String, Integer> map1 = new HashMap<>();
        HashMap<String, Integer> map2 = new HashMap<>();
        HashSet<String> set1 = new HashSet<>();
        HashSet<String> set2 = new HashSet<>();

        for (int i = 0; i < list.size(); i++) {
            map1.put(list.get(i).getName(), i);
            set1.add(list.get(i).getName());
        }
        for (int i = 0; i < list2.size(); i++) {
            map2.put(list2.get(i).getName(), i);
            set2.add(list2.get(i).getName());
        }
        Set<String> set = new HashSet<>(set1);
        set.addAll(set1);
        set.retainAll(set2);
        for (String name : set) {
            Integer index = map1.get(name);
            Integer index2 = map2.get(name);
            UserInfoPO pig = list.get(index);
            UserInfoPO pig2 = list2.get(index2);
            pig2.setImage(pig.getImage());
        }
        for (UserInfoPO pig : list2) {
            System.out.println(pig);
        }
    }

    private static void test0001() {
        String successTime = "20231226142210";
        DateTime loanTime = StrUtil.isNotBlank(successTime) ? DateUtil.parse(successTime, DatePattern.PURE_DATETIME_PATTERN) : DateUtil.date();
        System.out.println(loanTime.toString());
        DateTime date = DateUtil.parse(successTime, DatePattern.PURE_DATETIME_PATTERN);
        String a = ObjectUtil.isNull(date) ? DateUtil.date().toString() : date.toString(DatePattern.NORM_DATETIME_PATTERN);
    }


}
